package cn.syc.struck.demo.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.syc.struck.demo.R;
import cn.syc.struck.demo.bean.AppInfo;

/**
 * Created by czz on 15/11/26.
 */
public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ViewHolder> {
    public List<AppInfo> datas = null;

    public AppAdapter(List<AppInfo> datas) {
        this.datas = datas;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        return new ViewHolder(view);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.mView.setBackground(datas.get(position).getAppIcon());
        viewHolder.mTextView.setText(datas.get(position).getAppName());
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View mView;
        public TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            mView = (View) view.findViewById(R.id.view);
            mTextView = (TextView) view.findViewById(R.id.text);
        }
    }
}
